import { Button, Card, Form, Input, Modal, Popconfirm, Space, Select } from 'antd';
import Table, { ColumnsType } from 'antd/es/table';
import { TableRowSelection } from 'antd/es/table/interface';
import { useEffect } from 'react';

import { IconButton, Iconify } from '@/components/icon';
import { useCommon } from '@/hooks/common';

export default function ExpressListPage() {
  const uri = 'distribution-mode';
  const cateName = '配送方式列表';
  const {
    data = {},
    query,
    ModalPros,
    onCreate,
    doDel,
    onEdit,
    handleChange,
  } = useCommon({}, uri, {
    name: '',
    id: 0,
    telphone: '',
    isChina: 1,
    sort: 1,
    status: 0,
  });

  const columns: ColumnsType<any> = [
    { title: 'ID', dataIndex: 'id', width: 50 },
    { title: '快递公司', dataIndex: 'name', width: 100 },
    { title: '官方电话', dataIndex: 'telphone', align: 'center', width: 100 },
    {
      title: '国内国外',
      dataIndex: 'isChina',
      align: 'center',
      width: 120,
      render: (isChina) => <div>{isChina === 1 ? '国内' : '国外'}</div>,
    },
    {
      title: '操作',
      key: 'operation',
      align: 'center',
      width: 100,
      render: (_, record) => (
        <div className="flex w-full justify-center text-gray">
          <IconButton onClick={() => onEdit(record)}>
            <Iconify icon="solar:pen-bold-duotone" size={18} />
          </IconButton>
          <Popconfirm
            title="确认删除该数据？"
            okText="确定"
            cancelText="取消"
            placement="left"
            onConfirm={() => doDel(record.id)}
          >
            <IconButton>
              <Iconify icon="mingcute:delete-2-fill" size={18} className="text-error" />
            </IconButton>
          </Popconfirm>
        </div>
      ),
    },
  ];

  // rowSelection objects indicates the need for row selection
  const rowSelection: TableRowSelection<any> = {
    onChange: (selectedRowKeys, selectedRows) => {
      console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
    },
    onSelect: (record, selected, selectedRows) => {
      console.log(record, selected, selectedRows);
    },
    onSelectAll: (selected, selectedRows, changeRows) => {
      console.log(selected, selectedRows, changeRows);
    },
  };

  return (
    <Space direction="vertical" size="large" className="w-full">
      {/* <Card>
        <Form form={searchForm} onFinish={onSearch}>
          <Row gutter={[16, 16]}>
            <Col span={8} lg={6}>
              <Form.Item<any> label="状态" name="status" className="!mb-0">
                <Select
                  options={[
                    { label: '全部', value: '' },
                    { label: '已上架', value: 0 },
                    { label: '已下架', value: 1 },
                  ]}
                />
              </Form.Item>
            </Col>
            <Col span={24} lg={12}>
              <div className="flex justify-end">
                <Button onClick={onSearchFormReset}>重置</Button>
                <Button type="primary" htmlType="submit" className="ml-4">
                  查询
                </Button>
              </div>
            </Col>
          </Row>
        </Form>
      </Card> */}

      <Card
        title={cateName}
        extra={
          <div className="flex flex-row gap-[10px]">
            <Button
              type="primary"
              onClick={() =>
                onCreate({ parentId: query.parentId, level: query.parentId === 0 ? 1 : 2 })
              }
            >
              新增
            </Button>
          </div>
        }
      >
        <Table
          rowKey="id"
          size="small"
          pagination={{ total: data.totalCount }}
          scroll={{ x: 'max-content' }}
          columns={columns}
          dataSource={data.list}
          rowSelection={{ ...rowSelection }}
          onChange={handleChange}
        />
      </Card>

      <CommonModal {...ModalPros} />
    </Space>
  );
}

function CommonModal({ title, show, formValue, onOk, onCancel }: any) {
  const [form] = Form.useForm();
  useEffect(() => {
    form.setFieldsValue({ ...formValue });
  }, [formValue, form]);
  return (
    <Modal
      title={title}
      open={show}
      onOk={() => onOk({ ...formValue, ...form.getFieldsValue() })}
      onCancel={onCancel}
    >
      <Form
        initialValues={formValue}
        form={form}
        labelCol={{ span: 4 }}
        wrapperCol={{ span: 18 }}
        layout="horizontal"
      >
        <Form.Item<any> label="公司名字" name="name" required>
          <Input />
        </Form.Item>
        <Form.Item<any> label="官方电话" name="telphone" required>
          <Input />
        </Form.Item>
        <Form.Item<any> label="国内国外" name="isChina" required>
          <Select
            options={[
              { label: '国内', value: 1 },
              { label: '国外', value: 2 },
            ]}
          />
        </Form.Item>
      </Form>
    </Modal>
  );
}
